import urls from '/util/urls'
import request from '/util/request'
import { alert } from '/util/utils'

const app = getApp()

Page({
  data: {
    id:'',
    // 附件
    attachment:[]
  },
  onLoad(options) {
    let that = this
    // 传递的参数
    const id = options.id
    that.setData({
      id:id,
    })
  },
  deleteImg(e){
    const that = this
     my.showActionSheet({
      title: '是否删除',
      items: ['删除'],
      cancelButtonText: '取消',
      success: (res) => {
        if(res.index != -1) {
          const fsId = e.target.dataset.field
          const one = that.data.attachment.find(t => t.fsId == fsId)
          const attachment = that.data.attachment.filter(t => t.fsId != fsId)
          if(one){
            request.delete(urls.inspectionRegularDeleteAttachmentUrl(one.fsId,that.data.id),{}, function(data) {
              that.setData({attachment:attachment})
            },function(err){})
          }
        }
      },
      error(e){}
    }); 
  },
  onUpLoadImageFromCamera(){
    const that = this;
    dd.chooseImage({ /** 调取相册*/
      // sourceType: ['camera', 'album'],
      sourceType: ['camera', 'album'],
      count: 9 - that.data.attachment.length,
      success: (res) => {
        var filePaths = res.filePaths;
        if(filePaths){
          filePaths.forEach(path =>{
            dd.uploadFile({
              url: urls.inspectionRegularUploadUrl(),
              fileType: 'image',
              fileName: 'file',
              filePath: path,
              headers: {
                Authentication: app.globalData.token,
                'Content-Type': 'application/json;charset=UTF-8'
              },
              success: (response) => {
                const file = JSON.parse(response.data)
                const obj = {
                  fsId: file.data.fsId,
                  name: file.data.name,
                  description: path
                }

                if(that.data.attachment){
                  const fsIdArr = that.data.attachment.map(t => t.fsId)
                  if (!fsIdArr.includes(obj.fsId)) {
                    that.setData({
                      ['attachment['+that.data.attachment.length+']']:obj
                    })

                  }
                }

              },
            });
          })
        }
      },
      fail: (e) => {
        console.log('fail----------------------')
      }
    })
  },
  
  onSubmit() {
    const that = this
    const {
      id,
      attachment
    } = this.data

    if(attachment.length <= 0){
      alert('请选择要上传附件')
      return false
    }

    const formData =attachment.map(e=>{return {fsId:e.fsId,name:e.name}})

    dd.showLoading({
      content: '提交中...',
    })
    request.put(urls.updateTaskAttachmentUrl(id), formData, () => {
      dd.hideLoading()
      dd.showToast({
        type: 'success',
        content: '提交成功',
        duration: 2000,
        success: () => {
          dd.redirectTo({
            url: '/pages/inspection/table/table?type=regular'
          })
        },
      })
    }, () => {
      dd.hideLoading()
      dd.showToast({
        type: 'fail',
        content: '提交失败',
        duration: 2000,
      })
    })
  }
});
